# -*- coding: utf8 -*-
"""
全局配置
"""
from __future__ import print_function, unicode_literals
from copy import copy


# 异常类定义
class UrlError(RuntimeError):
    pass


class ArgLackError(RuntimeError):
    pass


class ArgExceptionError(RuntimeError):
    pass


class RetDirect(object):
    """
    成功时直接返回，错误返回错误消息
    """
    RET_SUCCESS = {
        'err_code': '00',
        'err_msg': '请求成功'
    }
    RET_URL_ERROR = {
        'err_code': '01',
        'err_msg': "请求的URL不存在"
    }
    RET_ARG_LACK_ERROR = {
        'err_code': '02',
        'err_msg': "缺少必须的参数"
    }
    RET_ARG_LOGIC_ERROR = {
        'err_code': '03',
        'err_msg': "参数内部逻辑处理错误"
    }
    RET_EXCEPTION = {
        'err_code': '04',
        'err_msg': "处理过程中发生异常",
        'err_exception': None,
    }
    RET_AUTH_ERROR = {
        'err_code': '05',
        'err_msg': "Signature验证失败"
    }

    def success(self, data):
        return data

    def urlerror(self):
        return self.RET_URL_ERROR

    def arglack(self):
        return self.RET_ARG_LACK_ERROR

    def arglogic(self):
        return self.RET_ARG_LOGIC_ERROR

    def exception(self, msg):
        ret = copy(self.RET_EXCEPTION)
        ret['err_exception'] = msg
        return ret

    def autherror(self):
        return self.RET_AUTH_ERROR

    def code(self, result):
        return result.get('err_code')

    def msg(self, result):
        return result.get('err_msg')


class RetDictData(RetDirect):
    """
    返回字典{'data': data, err_code:'00', err_msg: 'something'}
    """

    def success(self, data):
        ret = copy(self.RET_SUCCESS)
        ret['data'] = data
        return ret


class RetDict(RetDirect):
    """
    在结果为字典的时候直接返回 {key: value, err_code:'00', err_msg: 'something'}
    """
    def success(self, data):
        ret = copy(self.RET_SUCCESS)
        ret.update(data)
        return ret
